#include "ServiceProvider.h"
#include "GameControls.h"
#include "MenuManager.h"
#include "PrintUtilities.h"
#include "Menus.h"

#include <cassert>
#include <iostream>

MenuManager::MenuManager(ServiceProvider* i_sp, GameControls* i_gc)
	: m_sp(i_sp)
	, m_gc(i_gc)
	, m_currentMenu(0)	
{
	assert(i_sp && i_gc);

	m_startingMenu			= new StartingMenu(this);
	m_teamSelectionMenu		= new TeamSelectionMenu(this);
	m_mainMenu				= new MainMenu(this);
	m_teamsManagementMenu	= new TeamsManagementMenu(this);
	m_playerSelectionMenu	= new PlayerSelectionMenu(this);
	m_classificationMenu	= new ClassificationMenu(this);
	m_turnsMenu				= new TurnsMenu(this);
	m_teamsStatsMenu		= new TeamsStatsMenu(this);
	m_editFormationMenu		= new EditFormationMenu(this);
	m_playTurnsMenu			= new PlayTurnsMenu(this);
	m_marketMenu			= new MarketMenu(this);
	m_loadMenu				= new LoadMenu(this);
	m_saveMenu				= new SaveMenu(this);
	m_endLeagueMenu			= new EndLeagueMenu(this);
	m_confirmPanelMenu		= new ConfirmPanelMenu(this);
	m_gameOverMenu			= new GameOverMenu(this);
	m_playersStatsMenu		= new PlayersStatsMenu(this);
	m_playersStatsShowMenu	= new PlayersStatsShowMenu(this);
}

MenuManager::~MenuManager()
{
	delete m_startingMenu;
	delete m_teamSelectionMenu;
	delete m_mainMenu;
	delete m_teamsManagementMenu;
	delete m_playerSelectionMenu;
	delete m_classificationMenu;
	delete m_turnsMenu;
	delete m_teamsStatsMenu;
	delete m_editFormationMenu;
	delete m_playTurnsMenu;
	delete m_marketMenu;
	delete m_loadMenu;
	delete m_saveMenu;
	delete m_endLeagueMenu;
	delete m_confirmPanelMenu;
	delete m_gameOverMenu;
	delete m_playersStatsMenu;
	delete m_playersStatsShowMenu;
}

void MenuManager::PushMenu(Menu* i_menu)
{
	m_menuStack.push_back(m_currentMenu);
	SetMenu(i_menu);
}

void MenuManager::PopMenu()
{
	if (m_menuStack.empty())
	{
		if(m_currentMenu)
			m_currentMenu->OnExit();
		m_gc->Quit();
	}
	else
	{
		SetMenu(m_menuStack.back());
		m_menuStack.pop_back();
	}
}

void MenuManager::SetMenu(Menu *i_menu)
{
	if(m_currentMenu)
		m_currentMenu->OnExit();
	m_currentMenu = i_menu;
	m_currentMenu->OnEnter();
}

void MenuManager::Run()
{
	assert(m_currentMenu != 0);

	bool goodInput = false;

	while(!goodInput)
	{
		Print::ClearScreen();
		Print::ResetInput();
		
		m_currentMenu->Draw();

		std::string input;
		std::cin >> input;

		goodInput = m_currentMenu->Input(input);

		if (!goodInput) 
			std::cin.clear();
	}
}


